package com.citywithincity.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

/**
 * 从HttpServletRequest中取出数据
 * 
 * @author Randy
 *
 */
public class RequestUtil {
	

	public static String[] getArray(HttpServletRequest request, String key) {
		String[] params = request.getParameterMap().get(key);
		return params;
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	public static Map<String, String> requestToMap(HttpServletRequest request) {
		Map<String, String> result = new HashMap<String, String>();
		for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
			result.put(entry.getKey(), entry.getValue()[0]);
		}
		return result;
	}

	/**
	 * 获取整形
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	public static int getInt(HttpServletRequest request, String key) {
		String value = request.getParameter(key);
		if (value == null)
			return 0;
		return Integer.parseInt(value);
	}

	public static long getLong(HttpServletRequest request, String key) {
		String value = request.getParameter(key);
		if (value == null)
			return 0;
		return Long.parseLong(value);
	}

	public static float getFloat(HttpServletRequest request, String key) {
		String value = request.getParameter(key);
		if (value == null)
			return 0;
		return Float.parseFloat(value);
	}

	public static double getDouble(HttpServletRequest request, String key) {
		String value = request.getParameter(key);
		if (value == null)
			return 0;
		return Double.parseDouble(value);
	}

	public static boolean getBoolean(HttpServletRequest request, String key) {
		String value = request.getParameter(key);
		if (value == null)
			return false;
		if (value.equals(""))
			return false;
		if (value.equals("0"))
			return false;
		if (value.equals("false"))
			return false;
		return true;
	}

	public static void mapToRequest(Map<String, Object> data,HttpServletRequest request) {
		for (Entry<String, Object> entry : data.entrySet()) {
			request.setAttribute(entry.getKey(), entry.getValue());
		}
	}

	public static String getStr(HttpServletRequest request,String name) {
		return request.getParameter(name);
	}

	public static Map<String, String> getHeaders(HttpServletRequest request) {
		Enumeration<String> enumeration = request.getHeaderNames();
		Map<String, String> data = new HashMap<String, String>();
		while(enumeration.hasMoreElements()){
			String key = enumeration.nextElement();
			data.put(key, request.getHeader(key));
		}
		return data;
	}

	public static String readAsStr(HttpServletRequest request,String encoding) throws IOException {
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = null;
		try{
			reader = new BufferedReader(new InputStreamReader(request.getInputStream(), encoding));
			String line = null;
			while( (line = reader.readLine())  != null ){
				sb.append( line) ;
			}
			return sb.toString();
		}finally{
			IoUtil.close(reader);
		}
		
		
		
		
	}

}
